home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / ODF / Found / FWString / SLLocale.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  4.0 KB  |  142 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLLocale.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef SLLOCALE_H
  13. #include "SLLocale.h"
  14. #endif
  15.  
  16. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  17. #include <Script.h>
  18. #endif
  19.  
  20. #ifdef FW_BUILD_MAC
  21. #pragma segment Strings
  22. #endif
  23.  
  24. #ifdef FW_BUILD_WIN
  25. // include for GetCPInfo
  26. #include <winnls.h>
  27. #endif
  28.  
  29. //========================================================================================
  30. // FW_MacScriptIsSingleByte
  31. //========================================================================================
  32.  
  33. #ifdef FW_BUILD_MAC
  34. static FW_Boolean FW_MacScriptIsSingleByte(short script)
  35. {
  36.     static short gCachedScriptCode = smRoman;
  37.     static long gCachedScriptIsSingleByte = true;
  38.     if (script != gCachedScriptCode)
  39.     {
  40.         gCachedScriptCode = script;
  41.         long flags = GetScriptVariable(script, smScriptFlags);
  42.         gCachedScriptIsSingleByte = (flags & (1L<<smsfSingByte))!=0;
  43.     }
  44.     return gCachedScriptIsSingleByte;
  45. }
  46. #endif
  47.  
  48. //========================================================================================
  49. // FW_WinCodePageIsSingleByte
  50. //========================================================================================
  51.  
  52. #ifdef FW_BUILD_WIN
  53. FW_Boolean FW_WinCodePageIsSingleByte(short codePage)
  54. {
  55.     switch (codePage)
  56.     {
  57.         case 874:            // Thai - one byte or two???
  58.             return true;
  59.  
  60.         case 932:            // Japanese Kanji
  61.         case 936:            // Chinese (PRC, Singapore)
  62.         case 949:            // Korean
  63.         case 950:            // Chinese (Taiwan, Hong Kong) 
  64.         case 1200:            // Unicode (BMP of ISO 10646)
  65.             return false;
  66.  
  67.         case 1252:            // Western Europe - Roman (Latin 1)
  68.         case 1250:            // Eastern Europe - Roman
  69.         case 1251:            // Eastern Europe - Cyrillic
  70.         case 1253:            // Greek
  71.         case 1254:            // Turkish
  72.         case 1255:            // Hebrew
  73.         case 1256:            // Arabic
  74.         case 1257:            // Baltic
  75.             return true;
  76.     }
  77.     return true;
  78. }
  79. #endif
  80.  
  81. //========================================================================================
  82. // FW_WinScriptIsSingleByte
  83. //========================================================================================
  84.  
  85. #ifdef FW_BUILD_WIN
  86. static FW_Boolean FW_WinScriptIsSingleByte(short codePage)
  87. {
  88.     static short gCachedCodePage = FW_kWinLatin1;
  89.     static short gCachedCodePageIsSingleByte = true;    // don't want to make this a long, like gCachedScriptIsSingleByte
  90.     if (codePage != gCachedCodePage)
  91.     {
  92.         gCachedCodePage = codePage;
  93.         CPINFO cpinfo;
  94.         if (::GetCPInfo((UINT)codePage, &cpinfo))        // Get code page info, if available
  95.             gCachedCodePageIsSingleByte = (cpinfo.MaxCharSize == 1);
  96.         else    // Error occurred - try a different tack
  97.             gCachedCodePageIsSingleByte = FW_WinCodePageIsSingleByte(codePage);
  98.     }
  99.     return gCachedCodePageIsSingleByte;
  100. }
  101. #endif
  102.  
  103. //========================================================================================
  104. // FW_LocaleIsSingleByte
  105. //========================================================================================
  106.  
  107. FW_Boolean FW_LocaleIsSingleByte(FW_Locale locale)
  108. {
  109. #ifdef FW_BUILD_MAC
  110.     return FW_MacScriptIsSingleByte(locale.fScriptCode);
  111. #elif defined FW_BUILD_WIN
  112.     return FW_WinScriptIsSingleByte(locale.fScriptCode);
  113. #endif
  114. }
  115.  
  116. //========================================================================================
  117. // FW_CharIsDoubleByte
  118. //========================================================================================
  119.  
  120. FW_Boolean FW_CharIsDoubleByte(const char* p, short offset, const FW_Locale& locale)
  121. {
  122.     FW_Boolean result = false;    // assume false, which will be the case for single-byte scripts
  123.  
  124. #ifdef FW_BUILD_MAC
  125.     if (!FW_LocaleIsSingleByte(locale))
  126.     {
  127.         Ptr textBuf = (Ptr) p;
  128.         short byteType = ::CharacterByteType(textBuf, offset, locale.fScriptCode);
  129.         if (byteType == smFirstByte)
  130.             result = true;
  131.     }
  132. #endif
  133. #ifdef FW_BUILD_WIN
  134.     // Assume entire text uses the same code page
  135.     if (!FW_WinScriptIsSingleByte(locale.fScriptCode))
  136.         result = true;
  137. #endif
  138.  
  139.     return result;
  140. }
  141.  
  142.